package com.wangp.pdfcreator;

import java.io.FileOutputStream;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

public class PDFCreator3 {
	/**
	 * Itext 的基本使用 内容注释
	 *
	 * Document ： PDF对象 Rectangle ：页面对象 Table、PdfPTable ： 表格对象 Phrase短语对象 : a
	 * List of Chunks with leading Paragraph : a Phrase with extra properties
	 * and a newline ，新段落另起一行 PdfPCell ：单元格对象
	 *
	 */
	public static void main(String[] args) throws Exception {

		String outPutPath = "D:/test1.pdf";

		// 写出文件的位置
		FileOutputStream fos = new FileOutputStream(outPutPath);

		// 1-创建文本对象 Document
		Document document = new Document();

		// 2-初始化 pdf输出对象 PdfWriter
		PdfWriter writer = PdfWriter.getInstance(document, fos);

		Float pageHeight = PageSize.A4.getHeight();
		Float pageWidth = PageSize.A4.getWidth();

		// 3-打开 Document
		document.open();

		System.out.println("page高度=" + pageHeight + "，  宽度" + pageWidth + "  ---document--" + document.getPageSize());

		String imgPath = PDFCreator3.class.getResource("/1.png").toString();

		Image tImgCover = Image.getInstance(imgPath);

		float width = tImgCover.getWidth();
		float height = tImgCover.getHeight();
		/* 设置图片的位置 */
		tImgCover.setAbsolutePosition(-(0.75f * width - pageWidth) / 2, 680);
		/* 设置图片的大小 */
		tImgCover.scaleAbsolute(0.75f * width, 0.75f * height);

		PdfContentByte under = writer.getDirectContentUnder();
		under.beginText();
		under.addImage(tImgCover); // 加载图片
		under.endText();

		// 楷体
		String fontKai = PDFCreator3.class.getResource("/SIMKAI.TTF").toString();
		// 宋体
		// String fontSong =
		// PDFCreator3.class.getResource("/SIMSUN.TTC").toString();
		// Arial
		String fontEn = PDFCreator3.class.getResource("/arial.ttf").toString();

		addNewLine(document, 1);
		// BaseFont-确认支持中文,默认字体
		// BaseFont bfBasic = BaseFont.createFont("STSongStd-Light",
		// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		// 宋体 windows
		// BaseFont bfSong = BaseFont.createFont(fontSong + ",1",
		// BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		// 楷体
		BaseFont bfKai = BaseFont.createFont(fontKai, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		// Arial
		BaseFont bfEn = BaseFont.createFont(fontEn, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

		// 请您在办理入住登记时出示此凭证
		Font kaiNormal_9 = new Font(bfKai, 9, Font.NORMAL);
		Paragraph headTip = new Paragraph("请您在办理入住登记时出示此凭证", kaiNormal_9);
		headTip.setAlignment(Paragraph.ALIGN_CENTER);
		headTip.setSpacingBefore(74);
		document.add(headTip);

		// Title
		Font kaiBold_30 = new Font(bfKai, 30, Font.BOLD);
		Paragraph title = new Paragraph("入住凭证", kaiBold_30);
		title.setAlignment(Paragraph.ALIGN_CENTER);
		document.add(title);

		// 增加一行空白
		addNewLine(document, 1);

		// 副标题
		Font kaiBold_10 = new Font(bfKai, 10, Font.BOLD);
		Paragraph viceTitle1 = new Paragraph("预定信息", kaiBold_10);
		viceTitle1.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle1.setSpacingAfter(0);
		document.add(viceTitle1);

		// 横线
		Paragraph line = new Paragraph();
		line.add(new Chunk(new LineSeparator()));
		line.setSpacingBefore(-12);
		document.add(line);

		Font kaiNormal_10 = new Font(bfKai, 10, Font.NORMAL);
		Font enNormal_10 = new Font(bfEn, 10, Font.NORMAL);
		PdfPTable tableDetail = new PdfPTable(new float[] { 2, 3, 2, 3 });
		tableDetail.setWidthPercentage(90);
		tableDetail.setHorizontalAlignment(Element.ALIGN_CENTER);

		tableDetail.addCell(createCell("入住日期：", kaiBold_10, true));

		tableDetail.addCell(createCell("2018-11-15", enNormal_10, true));

		tableDetail.addCell(createCell("离店日期：", kaiBold_10, true));

		tableDetail.addCell(createCell("2018-11-16", enNormal_10, true));

		tableDetail.addCell(createCell("酒店房型：", kaiBold_10, true));

		tableDetail.addCell(createCell("海景房", kaiNormal_10, true));

		tableDetail.addCell(createCell("客房床型：", kaiBold_10, true));

		tableDetail.addCell(createCell("大床房：", kaiNormal_10, true));

		tableDetail.addCell(createCell("膳食套餐：", kaiBold_10, true));

		tableDetail.addCell(createCell("含早餐", kaiNormal_10, true));

		tableDetail.addCell(createCell("预定间数：", kaiBold_10, true));

		tableDetail.addCell(createCell("3", enNormal_10, true));

		tableDetail.addCell(createCell("住客姓名：", kaiBold_10, true));

		tableDetail.addCell(createCell("ZHANG/SAN" + System.getProperty("line.separator") + "LI/SI"
				+ System.getProperty("line.separator") + "WANG/WU" + System.getProperty("line.separator") + "WANG/WU"
				+ System.getProperty("line.separator") + "WANG/WU" + System.getProperty("line.separator") + "WANG/WU"
				+ System.getProperty("line.separator") + "WANG/WU", enNormal_10, false));

		tableDetail.addCell(createCell("确认号：", kaiBold_10, true));

		tableDetail.addCell(createCell("123456789", enNormal_10, false));

		document.add(tableDetail);

		// 添加一行空白
		addNewLine(document, 1);

		// 副标题
		Paragraph viceTitle2 = new Paragraph("酒店信息", kaiBold_10);
		viceTitle2.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle2.setSpacingAfter(0);
		document.add(viceTitle2);

		// 横线
		document.add(line);

		// 第二个表格
		PdfPTable tableHotel = new PdfPTable(new float[] { 2, 8 });
		tableHotel.setWidthPercentage(90);
		tableHotel.setHorizontalAlignment(Element.ALIGN_CENTER);
		//
		tableHotel.addCell(createCell("酒店名称：", kaiBold_10, true));

		tableHotel.addCell(createCell("上海市龙之梦大酒店", kaiNormal_10, false));

		tableHotel.addCell(createCell("酒店地址：", kaiBold_10, true));

		tableHotel.addCell(createCell("上海市松江区洞泾镇沈砖公路5160弄50支弄9号909室", kaiNormal_10, false));

		tableHotel.addCell(createCell("酒店电话：", kaiBold_10, true));

		tableHotel.addCell(createCell("021-88888888", enNormal_10, false));

		document.add(tableHotel);

		// 添加一行空白
		addNewLine(document, 1);

		// 副标题
		Paragraph viceTitle3 = new Paragraph("注意事项", kaiBold_10);
		viceTitle3.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle3.setSpacingAfter(0);
		document.add(viceTitle3);

		// 横线
		document.add(line);

		// 第三个表格
		PdfPTable tableAttention = new PdfPTable(1);
		tableAttention.setWidthPercentage(90);
		tableAttention.setHorizontalAlignment(Element.ALIGN_CENTER);

		tableAttention.addCell(createCell("登记入住时间：15:00 起" + System.getProperty("line.separator") + "退房离店时间：12:00 止",
				kaiNormal_10, false));

		tableAttention.addCell(createCell("订单一经确认，不可取消、修改。若未按时入住，我们将扣除您全额或部分房费。", kaiNormal_10, false));

		document.add(tableAttention);

		/****************************************************************/

		document.newPage();

		under.beginText();
		under.addImage(tImgCover); // 加载图片
		under.endText();

		addNewLine(document, 1);

		// 英文页面
		Font enNormal_9 = new Font(bfEn, 9, Font.NORMAL);
		Paragraph headTip_en = new Paragraph("Please present it upon arrival.", enNormal_9);
		headTip_en.setAlignment(Paragraph.ALIGN_CENTER);
		headTip_en.setSpacingBefore(74);
		document.add(headTip_en);

		// Title
		Font enBold_30 = new Font(bfEn, 30, Font.BOLD);
		Paragraph title_en = new Paragraph("VOUCHER", enBold_30);
		title_en.setAlignment(Paragraph.ALIGN_CENTER);
		document.add(title_en);

		// 增加一行空白
		addNewLine(document, 1);

		// 副标题
		Font enBold_10 = new Font(bfEn, 10, Font.BOLD);
		Paragraph viceTitle1_en = new Paragraph("Booking Information", enBold_10);
		viceTitle1_en.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle1_en.setSpacingAfter(0);
		document.add(viceTitle1_en);

		document.add(line);

		PdfPTable tableDetail_en = new PdfPTable(new float[] { 2, 3, 2, 3 });
		tableDetail_en.setWidthPercentage(90);
		tableDetail_en.setHorizontalAlignment(Element.ALIGN_CENTER);

		tableDetail_en.addCell(createCell("Check-In Date:", enBold_10, true));

		tableDetail_en.addCell(createCell("2018-11-15", enNormal_10, true));

		tableDetail_en.addCell(createCell("Check-Out Date:", enBold_10, true));

		tableDetail_en.addCell(createCell("2018-11-16", enNormal_10, true));

		tableDetail_en.addCell(createCell("Room Type:", enBold_10, true));

		tableDetail_en.addCell(createCell("Deluxe Room", enNormal_10, true));

		tableDetail_en.addCell(createCell("Bed Type:", enBold_10, true));

		tableDetail_en.addCell(createCell("2 Single beds", enNormal_10, true));

		tableDetail_en.addCell(createCell("Meals:", enBold_10, true));

		tableDetail_en.addCell(createCell("Room only", enNormal_10, true));

		tableDetail_en.addCell(createCell("No. of Room(s):", enBold_10, true));

		tableDetail_en.addCell(createCell("3", enNormal_10, true));

		tableDetail_en.addCell(createCell("Guest:", enBold_10, true));

		tableDetail_en.addCell(createCell("ZHANG/SAN" + System.getProperty("line.separator") + "LI/SI"
				+ System.getProperty("line.separator") + "WANG/WU", enNormal_10, false));

		tableDetail_en.addCell(createCell("Confirmation No:", enBold_10, true));

		tableDetail_en.addCell(createCell("123456789", enNormal_10, false));

		document.add(tableDetail_en);

		// 添加一行空白
		addNewLine(document, 1);

		// 副标题
		Paragraph viceTitle2_en = new Paragraph("Hotel Information", enBold_10);
		viceTitle2_en.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle2_en.setSpacingAfter(0);
		document.add(viceTitle2_en);

		// 横线
		document.add(line);

		// 第二个表格
		PdfPTable tableHotel_en = new PdfPTable(new float[] { 2, 8 });
		tableHotel_en.setWidthPercentage(90);
		tableHotel_en.setHorizontalAlignment(Element.ALIGN_CENTER);

		tableHotel_en.addCell(createCell("Hotel Name:", enBold_10, true));

		tableHotel_en.addCell(createCell("Disneyland Hotel", enNormal_10, false));

		tableHotel_en.addCell(createCell("Hotel Address:", enBold_10, true));

		tableHotel_en.addCell(
				createCell("Disneyland Resort, Lantau Island Disneyland Area Hong Kong China", enNormal_10, false));

		tableHotel_en.addCell(createCell("Hotel Tel:", enBold_10, true));

		tableHotel_en.addCell(createCell("021-88888888", enNormal_10, false));

		document.add(tableHotel_en);

		// 添加一行空白
		addNewLine(document, 1);

		// 副标题
		Paragraph viceTitle3_en = new Paragraph("Attention", enBold_10);
		viceTitle3_en.setAlignment(Paragraph.ALIGN_CENTER);
		viceTitle3_en.setSpacingAfter(0);
		document.add(viceTitle3_en);

		// 横线
		document.add(line);

		// 第三个表格
		PdfPTable tableAttention_en = new PdfPTable(1);
		tableAttention_en.setWidthPercentage(90);
		tableAttention_en.setHorizontalAlignment(Element.ALIGN_CENTER);

		tableAttention_en.addCell(
				createCell("Check in time 15:00" + System.getProperty("line.separator") + "Check out time 11:00",
						enNormal_10, false));

		tableAttention_en.addCell(createCell(
				"The order cannot be modified and renamed. If you do not check in on time, full or part of the reservation amount will be charged as cancellation fee. ",
				enNormal_10, false));

		document.add(tableAttention_en);
		// 关闭 Document
		document.close();

	}

	private static void addNewLine(Document doc, int line) {
		try {
			for (int i = 0; i < line; i++) {
				doc.add(Chunk.NEWLINE);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	private static PdfPCell createCell(String content, Font font, boolean noWrap) {
		return createCell(content, font, noWrap, 0, 0);
	}

	private static PdfPCell createCell(String content, Font font, boolean noWrap, int border, int align) {
		PdfPCell cell = new PdfPCell();
		cell.setBorder(border);
		// ALIGN_LEFT = 0 ,ALIGN_CENTER = 1 , ALIGN_RIGHT = 2
		cell.setHorizontalAlignment(align);
		// Bold == 1 normal == 0
		cell.addElement(new Phrase(content, font));
		cell.setNoWrap(noWrap);

		return cell;
	}

}
